<?php
namespace App\Http\Controllers\Admin\Erqi;

use App\Service\Admin\Erqi\SucaiService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class SucaiController extends BaseController
{
    //素材

    public function __construct(SucaiService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getIndex(Request $request)
    {
        $type = $request->post('type', 0);
        $res = $this->db->getList($type);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //文件列表
    public function getFiles()
    {
        $res = $this->db->getFiles();
        return $this->toJson(0, '', $res);
    }

    //详情
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //更新
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $cnt = $request->post('cnt', '');
        $type = $request->post('type', 0);
        $isImg = $request->post('isImg', 0);
        $isVideo = $request->post('isVideo', 0);
        if (!$cnt && !$isImg && !$isVideo) {
            return $this->toJson(-2, '文字、图片或视频必须的有一个');
        }
        if ($id) {
            $res = $this->db->modify($id, $cnt, $type);
        } else {
            $res = $this->db->add($cnt, $type);
        }
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson();
    }

    //上传图片
    public function uploadImg(Request $request)
    {
        $scid = $request->post('id', 0);
        $img = $request->post('img', '');
        if (!$img) {
            return $this->toJson(-2, '图片必须的');
        }
        $fpath = Upload::getPathByBase64($img);
        $res = $this->db->addFile($scid, 1, $fpath);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $fpath = '/'.$fpath;
        return $this->toJson(0, '', $fpath);
    }

    //上传视频
    public function uploadVideo(Request $request)
    {
        ini_set('upload_max_filesize', '100M');
        ini_set('post_max_size', '100M');
        $scid = $request->post('scid', 0);
        $upload = Upload::uploadVideo($request, 1, 1, time());
        if ($upload['code'] < 0) {
            return $this->toJson(-2, $upload['msg']);
        }
        $fpath = $upload['data'];
        $res = $this->db->addFile($scid, 2, $fpath);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $fpath = '/'.$fpath;
        return $this->toJson(0, '', $fpath);
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson();
    }
}